package com.netscenery.yang.model;

import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;
import com.netscenery.yang.parser.YangParserException;

public class SubModule extends Module {
	private BelongsTo belongsto;
	
	/**
	 * 
	 * @param env
	 * @param name
	 * @throws ModelException 
	 */
	public SubModule(Env env,String name) throws ModelException{
		super(env,name);
		Env thisEnv = this.getEnv();
		if (null != thisEnv ){
			thisEnv.setCurModule(this);
		}
		
	}
	/**
	 * 
	 * @param env
	 * @param name
	 * @param belongsto
	 * @throws ModelException 
	 */
	public SubModule(Env env,String name, BelongsTo belongsto) throws ModelException{
		super(env,name);
		
		if (null == belongsto){
			return;
		}
		if (null != env){
			if (null != env.getModule(name)){
				return;
			}
			if (null == env.getModule(belongsto.getName())){
				return;
			}
		}
		
		this.env = env;		
		this.name = name;
		this.belongsto = belongsto;
		Env thisEnv = this.getEnv();
		if (null != thisEnv ){
			thisEnv.setCurModule(this);
		}
	}

	public SubModule(Env env,String name,String belongsto,String prefix) throws ModelException{
		super(env,name);
		
		if (null == belongsto){
			return;
		}
		if (null != env){
			if (null != env.getModule(name)){
				return;
			}
			if (null == env.getModule(belongsto)){
				return;
			}
		}
		BelongsTo belongs = new BelongsTo(env,belongsto,prefix);
		this.env = env;		
		this.name = name;
		this.belongsto = belongs;
		Env thisEnv = this.getEnv();
		if (null != thisEnv ){
			thisEnv.setCurModule(this);
		}
	}

	public BelongsTo getBelongsto() {
		return belongsto;
	}
	/**
	 * ����belongsto
	 * @param belongsto
	 */
	public void setBelongsto(BelongsTo belongsto) {
		this.belongsto = belongsto;
		if (null != env){
			env.addPrefix(belongsto.getPrefix());
		}
	}
	/**
	 * ����belongsto
	 * @param name
	 * @param prefix
	 * @return
	 */
	public BelongsTo createBelongsto(String name,String prefix){
		if (null == name){
			return null;
		}
		if (null == prefix){
			return null;
		}
		Env subEnv = newSubEnv();
		BelongsTo be = null;
		be = new BelongsTo(subEnv,name,prefix);
		
		return be;
	}

	@Override
	public Validator validate() {
		Validator validator = new Validator();
		if (isNullString(name) || !checkName(name)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this, ErrTag.ERROR,ErrorCode.COMMON_INVALIDNAME));
		}
		if (null == this.getBelongsto()){
			validator.setIsValid(false);
			validator.addMsg(new Message(this, ErrTag.ERROR,"belongsto is mandatory."));
		}
		else{
			BelongsTo belongs = this.getBelongsto();
			Validator bValidator = belongs.validate();
			validator.copy(bValidator);
		}
		
		
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}


	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
	
}
